package com.example.administrator.tenyuanpackage.home.mvp;

import android.content.Context;
import android.util.Log;

import com.example.administrator.tenyuanpackage.home.bean.PreferentialBean;
import com.example.administrator.tenyuanpackage.home.bean.ResultBean;
import com.example.administrator.tenyuanpackage.home.bean.ResultsGoodsBean;

import java.util.ArrayList;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static android.content.ContentValues.TAG;

/**
 * Created by zwq on 2017/2/27.
 */

public class HomePresentImpl implements IHomePresenter {

    private IHomeModel iHomeModel;
    private IHomeView iHomeView;
    private Context context;
    private int page=1;

    public HomePresentImpl(Context context, IHomeView iHomeView) {
        this.iHomeView = iHomeView;
        iHomeModel = new HomeModeImpl();
        this.context = context;
    }

    @Override
    public void getLoopData() {
        iHomeModel.getLoopObservabal().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("================", e.getMessage());
                    }

                    @Override
                    public void onNext(ResultBean loopBean) {

                        iHomeView.getLoopData(loopBean);
                    }
                });
    }

    @Override
    public void getPreferentials() {

        iHomeModel.getPreferentialObservabal().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Subscriber<ArrayList<PreferentialBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArrayList<PreferentialBean> preferentials) {
                        Log.i(TAG, "onNext: =============" + preferentials.size());
                        iHomeView.getPreferntialData(preferentials);
                    }
                });
    }

    @Override
    public void getGoodsData(int page) {
        iHomeModel.getGoodsObservabal(page).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResultsGoodsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("================", e.getMessage());
                    }

                    @Override
                    public void onNext(ResultsGoodsBean goodsBean) {
                        Log.i(TAG, "onNext: ==============" + goodsBean.getRows().size());
                        iHomeView.getGoodsData(goodsBean);
                    }
                });
    }

    @Override
    public void push() {
        page++;
        iHomeModel.getGoodsObservabal(page).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResultsGoodsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("================", e.getMessage());
                    }

                    @Override
                    public void onNext(ResultsGoodsBean goodsBean) {
                        Log.i(TAG, "onNext: ========="+goodsBean.getRows().size());
                        iHomeView.onPush(goodsBean);
                    }
                });
    }
}